//
//  BJBackGroundView.m
//  SZBank
//
//  Created by rimi on 16/7/12.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJBackGroundView.h"



@interface  BJBackGroundView ()
{
    NSInteger style;
}
@property (nonatomic)UIImageView *calcultionImageView;/**< 计算图片 */

@property (nonatomic)UILabel *calcultionImageViewTitleLabel;/**< 计算图片title */
@property (nonatomic)UILabel *calcultionImageViewTheme;/**< 计算图片的主题 */
@property (nonatomic)UITextField *calcultionImageViewMoneyTextField;/**< 计算图片金额输入框 */
@property (nonatomic)UITextField *calcultionImageVeiwRateTextField;/**< 计算图片利率输入框 */
@property (nonatomic)UISlider *calcultionImageViewSlider;/**< 计算图片的滑块 */

@property (nonatomic)NSDictionary *dataDictionary;/**< 数据源数组 */

@property (nonatomic)UIImageView *showImageView;/**< 展示图片 */

@end
@implementation BJBackGroundView

- (instancetype)initWithFrame:(CGRect)frame dataDictionary:(NSDictionary *)dataDictionary imageStyle:(ImageViewStyle) imageStyle tableStyle:(NSInteger)tableStyle
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataDictionary = dataDictionary;
        style = imageStyle;
        self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.675];
        //判断加入的视图
        switch (imageStyle) {
            case calculationStyle:
            {
                style = calculationStyle;
                [self addSubview:self.calcultionImageView];
            }
                break;
                case showImageStyle:
            {
                style = showImageStyle;
                [self creatTableWithTableStyle:tableStyle];
               
                [self addTextToShowImagesWithdataArray:dataDictionary tableStyle:tableStyle];

                [self addSubview:self.showImageView];
            }
                break;
            default:
                break;
        }
        
        
        
        
        
    }
    return self;
}


#pragma mark -- actions

#pragma mark -- 计算action
//点击开始（收集点击范围）
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self endEditing:YES];

    //获取触摸点
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
     //确定范围
    CGRect touchRect;
    if (style == 0) {
         touchRect = self.calcultionImageView.frame;
        if (!CGRectContainsPoint(touchRect, touchPoint)) {
            [self removeFromSuperview];
        }
    }

 
}

//添加视图到计算图片视图
- (void)addSubViewTocalcultaionImageViewWithDataDictionary:(NSDictionary *)dataDictionary{
    //投资期限
    UILabel *dayLabel = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH * 0.18 , KHEIGHT * 0.125, KWIDTH *0.1, KHEIGHT* 0.04)];
    dayLabel.text  = self.dataDictionary[@"labels"][2];
    dayLabel.font = [UIFont systemFontOfSize:30.0];
    //购买金额
    
  
    
    
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake( KWIDTH * 0.27, KHEIGHT*0.32, KWIDTH * 0.32 , KHEIGHT*0.075)];
    [button addTarget:self action:@selector(action_calcultionButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"对比_07"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:28.0];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(KWIDTH * 0.02, 0, 0, 0)];
    [self.calcultionImageView addSubview:button];
    
    
    
    [self.calcultionImageView addSubview:self.calcultionImageViewSlider];
    [self.calcultionImageView addSubview:self.calcultionImageVeiwRateTextField];
    [self.calcultionImageView addSubview:self.calcultionImageViewMoneyTextField];
    [self.calcultionImageView addSubview:dayLabel];
    [self.calcultionImageView addSubview:self.calcultionImageViewTitleLabel];
    [self.calcultionImageView addSubview:self.calcultionImageViewTheme];

    
}
//计算出收益
- (void)action_calcultionButton:(UIButton *)sender{
    
    CGFloat gold = [self.calcultionImageViewMoneyTextField.text doubleValue];
    CGFloat rate = [self.calcultionImageVeiwRateTextField.text doubleValue];
    CGFloat ti =[self.dataDictionary[@"labels"][2] doubleValue];

    if (gold < 5) {
        [LocalDataManager addAlertControllerWithControllerTitle:@"您输入的金额小于最小出售金额" controllerMessage:nil actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
    }else if(rate <= 0){
        [LocalDataManager addAlertControllerWithControllerTitle:@"您输入的利率不正确" controllerMessage:nil actionSureTitle:@"确定" actionCancelTile:nil actionSureBlock:nil actionCancelBlock:nil style:0];
    }else{
        [sender setTitle:[NSString stringWithFormat:@"%.2f元",(gold*rate*ti*10000/365/100)] forState:UIControlStateNormal];
    }
    
}
//获取slider的值，并赋给calcultionImageViewMoneyTextField
- (void)action_slider:(UISlider *)sender{
    self.calcultionImageViewMoneyTextField.text = [NSString stringWithFormat:@"%ld",((NSInteger)sender.value)];
    

}


#pragma mark -- 展示action

- (void)creatTableWithTableStyle:(NSInteger)tableStyle{
    for (int i = 0; i < 7; i ++) {
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(KWIDTH * 0.1 + KWIDTH * 0.13 * i, KHEIGHT * 0.3, KWIDTH * 0.003, KHEIGHT * 0.3)];
        line.backgroundColor = [UIColor colorWithRed:145/ 255.0 green:84/ 255.0 blue:17/ 255.0 alpha:1.000];
        [self.showImageView addSubview:line];
    }
    for (int i = 0; i < 3; i ++) {
        UIView *line =[[UIView alloc]init];
         line.backgroundColor = [UIColor colorWithRed:145/ 255.0 green:84/ 255.0 blue:17/ 255.0 alpha:1.000];
        if (i == 0) {
           line.frame= CGRectMake(KWIDTH * 0.1 , KHEIGHT * 0.3 +KHEIGHT* i * 0.1, KWIDTH * 0.78, KWIDTH * 0.003);
        }else if (i == 1){
           line.frame= CGRectMake(KWIDTH * 0.1 , KHEIGHT * 0.3 +KHEIGHT* i * 0.076, KWIDTH * 0.78, KWIDTH * 0.003);
        }else{
           line.frame= CGRectMake(KWIDTH * 0.1 , KHEIGHT * 0.597, KWIDTH * 0.78, KWIDTH * 0.003);
        }
        [self.showImageView addSubview:line];
    }
    
    //退出button
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(KWIDTH * 0.88, KHEIGHT * 0.11, KWIDTH * 0.04, KWIDTH * 0.04)];
    button.tag = 10000;
   [ button setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"理财详情_关闭"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(action_alertButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.showImageView addSubview:button];
    //石榴图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH* 0.65, KHEIGHT* 0.7, KWIDTH * 0.22, KHEIGHT * 0.3)];
    imageView.image = [LocalDataManager getImageWithSourceOfPath:@"理财_插图"];
    [self.showImageView addSubview:imageView];
    //底部label
    NSArray *str = @[@"苏州银行",@"2013-09-13",@"产品简介"];
    for (int i = 0; i < 3; i ++) {
        UILabel *label = [[UILabel alloc]init];
        if (i < 2) {
            label.frame = CGRectMake(CGRectGetMaxX(imageView.frame) - imageView.frame.size.width - KWIDTH * 0.2, KHEIGHT * 0.9 + KHEIGHT * 0.04 * i, KWIDTH * 0.2, KHEIGHT * 0.04);
        }else{
            label.frame = CGRectMake(KWIDTH * 0.06, KHEIGHT * 0.6, KWIDTH * 0.1, KHEIGHT * 0.04);
        }
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:FONT_SIZE];
        label.text = str[i];
        [self.showImageView addSubview:label];
    }
    //说明button
    UIButton *leaflet = [[UIButton alloc]initWithFrame:CGRectMake(KWIDTH* 0.1, KHEIGHT * 0.9, KWIDTH * 0.1, KHEIGHT * 0.06)];
    [leaflet setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [leaflet setTitle:@"说明书" forState:UIControlStateNormal];
    leaflet.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
    [leaflet addTarget:self action:@selector(action_alertButton:) forControlEvents:UIControlEventTouchUpInside];
    leaflet.tag = 10001;
    [self.showImageView addSubview:leaflet];
    
    //中间线条
    if (tableStyle == 1) {
        for (int i = 0; i < 6; i ++) {
            UIView *line =[[UIView alloc]init];
            line.backgroundColor = [UIColor colorWithRed:145/ 255.0 green:84/ 255.0 blue:17/ 255.0 alpha:1.000];
            if (i < 3) {
                 line.frame= CGRectMake(KWIDTH * 0.23 , KHEIGHT * 0.43 +KHEIGHT* i * 0.06, KWIDTH * 0.13, KWIDTH * 0.003);
            }else{
                 line.frame= CGRectMake(KWIDTH * 0.75 , KHEIGHT * 0.43 +KHEIGHT* (i - 3) * 0.06, KWIDTH * 0.13, KWIDTH * 0.003);
            }
            [self.showImageView addSubview:line];
        }
    }
    
    //产品概况label
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH * 0.1, KHEIGHT * 0.24, KWIDTH * 0.2, KHEIGHT * 0.06)];
    titleLabel.text = @"产品概况";
    titleLabel.textColor = [UIColor colorWithRed:145/ 255.0 green:84/ 255.0 blue:17/ 255.0 alpha:1.000];
    titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
    [self.showImageView addSubview:titleLabel];
}


/**
 *给展示图片添加label并且赋值
 */

- (void)addTextToShowImagesWithdataArray:(NSDictionary *)dataArray tableStyle:(NSInteger )tableStyle{
    
    
    
 
    
    //至
    UILabel *zhi = [self creatLabel];
    zhi.frame = CGRectMake(KWIDTH * 0.367, KHEIGHT  * 0.46 , KWIDTH * 0.12, KHEIGHT * 0.06);
    zhi.text = @"至";
    [self.showImageView addSubview:zhi];
  
    

//循环创建有相同属性的label
    NSArray *array1_7 = @[@"产品名称",@"起点金额",@"产品认购期",@"起息日",@"产品投资期限",@"预期年化收益"];
    for (int i = 0; i < 13; i ++) {
        
        UILabel *label = [self creatLabel];
        if (i == 0) {
            label.frame = CGRectMake(KWIDTH * 0.15, KHEIGHT * 0.12, KWIDTH * 0.7, KHEIGHT * 0.06);
            label.font = [UIFont systemFontOfSize:32.0];
            label.text = dataArray[@"nomal"][0];
        }else if (i == 1){
            label.frame = CGRectMake(KWIDTH * 0.1, KHEIGHT * 0.19, KWIDTH * 0.8, KHEIGHT * 0.06);
            label.textColor =[UIColor blackColor];
              label.textAlignment = NSTextAlignmentLeft;
             label.text = dataArray[@"nomal"][1];
        }else if (i > 1 && i < 8){
            label.frame = CGRectMake(KWIDTH * 0.105 + (i - 2) * KWIDTH * 0.13, KHEIGHT * 0.31, KWIDTH * 0.12, KHEIGHT * 0.06);
            label.text = array1_7[i - 2];

        }else if (i == 8){
            label.numberOfLines = 0;
            label.frame = CGRectMake(KWIDTH * 0.105, KHEIGHT * 0.42, KWIDTH * 0.12, KHEIGHT * 0.15);
             label.text = dataArray[@"nomal"][2];
        }else if (i > 8 && i <  11){
            label.frame = CGRectMake(KWIDTH * 0.367, KHEIGHT  * 0.39 + (i - 9) * KHEIGHT * 0.14, KWIDTH * 0.12, KHEIGHT * 0.06);
            label.text =dataArray[@"nomal"][i - 6];
        }
        else if (i < 13 && i > 10){
            label.frame = CGRectMake(KWIDTH * 0.367 + (i - 10) * KWIDTH * 0.13, KHEIGHT * 0.45, KWIDTH * 0.12, KHEIGHT * 0.06);
            label.text =dataArray[@"nomal"][i - 6];
        }
        
  
        
        [self.showImageView addSubview:label];
        
    }
  
    //根据style来创建label
    if (tableStyle == 0) {
        for (int i = 0; i < 2;  i ++) {
            UILabel *label = [self creatLabel];
            label.frame = CGRectMake(KWIDTH * 0.237 + i * KWIDTH * 0.52, KHEIGHT * 0.45, KWIDTH * 0.12, KHEIGHT * 0.06);
            label.text = label.text =dataArray[@"diffierent"][i];
            [self.showImageView addSubview:label];

        }
    }else{
        for (int i = 0; i < 8; i ++) {
            UILabel *label = [self creatLabel];

            if (i < 4) {
                label.frame = CGRectMake(KWIDTH * 0.235,  KHEIGHT * 0.38 +KHEIGHT* i * 0.055, KWIDTH * 0.12, KHEIGHT * 0.05);
                
            }else{
                label.frame = CGRectMake(KWIDTH * 0.755,  KHEIGHT * 0.38 +KHEIGHT* (i - 4)* 0.055, KWIDTH * 0.12, KHEIGHT * 0.05);
            }
            label.text = label.text =dataArray[@"diffierent"][i];
            [self.showImageView addSubview:label];
        }
    }
}

/**
 * 创建label
 */
- (UILabel *)creatLabel{
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:145/ 255.0 green:84/ 255.0 blue:17/ 255.0 alpha:1.000];
    label.font = [UIFont systemFontOfSize:FONT_SIZE];
    return label;
}
/**
 *  展示图片退出方法
 */
- (void)action_alertButton:(UIButton *)sender{
    if (sender.tag == 10000) {
           [sender.superview.superview removeFromSuperview];
    }else{
        NSLog(@"说明");
    }
    
}

#pragma mark -- getters




#pragma mark -- 收益试计算
/**
 *  计算图片视图
 *
 *  @return
 */

- (UIImageView *)calcultionImageView{
    if (!_calcultionImageView) {
        _calcultionImageView = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH * 0.18, KHEIGHT * 0.2, KWIDTH * 0.64, KHEIGHT * 0.4)];
            imageView.image = [LocalDataManager getImageWithSourceOfPath:@"试算_00"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });
        //添加视图
        
        [self addSubViewTocalcultaionImageViewWithDataDictionary:self.dataDictionary];
    }
    return _calcultionImageView;
}

/**
 *  计算图片title
 *
 *  @return
 */

- (UILabel *)calcultionImageViewTitleLabel{
    if (!_calcultionImageViewTitleLabel) {
        _calcultionImageViewTitleLabel = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH * 0.15 , KHEIGHT * 0.03, KWIDTH *0.4, KHEIGHT* 0.06)];
            label.textColor =  [UIColor colorWithRed:0.455 green:0.643 blue:0.239 alpha:1.000];
            label.text = self.dataDictionary[@"labels"][1];
            label.font = [UIFont systemFontOfSize:24.0];
            label;
        });
    }
    return _calcultionImageViewTitleLabel;
}
/**
 *计算图片的主题
 */


- (UILabel *)calcultionImageViewTheme{
    if (!_calcultionImageViewTheme) {
          NSString *themeString = self.dataDictionary[@"labels"][0];
        NSString *themeFirst = [themeString substringToIndex:1];
        NSString *themeSecond = [themeString substringWithRange:NSMakeRange(1, 1)];
        
        _calcultionImageViewTheme =({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH * 0.074 , KHEIGHT * 0.009, KWIDTH *0.044, KHEIGHT* 0.04)];
            label.textColor =  [UIColor colorWithRed:0.455 green:0.643 blue:0.239 alpha:1.000];
            label.text = themeFirst;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:35.0];
            label.textColor = [UIColor whiteColor];
            label;
        });
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH * 0.074 , KHEIGHT * 0.049, KWIDTH *0.044, KHEIGHT* 0.04)];
        label.textColor =  [UIColor colorWithRed:0.455 green:0.643 blue:0.239 alpha:1.000];
        label.text = themeSecond;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:35.0];
        label.textColor = [UIColor whiteColor];
        [self.calcultionImageView addSubview:label];

    }
    return _calcultionImageViewTheme;
}
/**
 *  计算图片的金额输入框
 *
 *
 *
 *  @return
 */

- (UITextField *)calcultionImageViewMoneyTextField{
    if (!_calcultionImageViewMoneyTextField) {
        _calcultionImageViewMoneyTextField = ({
            UITextField *moneyField = [[UITextField alloc]initWithFrame:CGRectMake(KWIDTH * 0.433,  KHEIGHT * 0.19, KWIDTH * 0.085, KHEIGHT * 0.04)];
            moneyField.font = [UIFont systemFontOfSize:24.0];
            moneyField.adjustsFontSizeToFitWidth = YES;
            moneyField;
        });
    }
    return _calcultionImageViewMoneyTextField;
}


/**
 *  计算图片利率输入框
 *
 */
- (UITextField *)calcultionImageVeiwRateTextField{
    if (!_calcultionImageVeiwRateTextField) {
        _calcultionImageVeiwRateTextField = ({
            UITextField *moneyField = [[UITextField alloc]initWithFrame:CGRectMake(KWIDTH * 0.186,  KHEIGHT * 0.25, KWIDTH * 0.115, KHEIGHT * 0.045)];
            moneyField.text  = self.dataDictionary[@"labels"][3];
            moneyField.adjustsFontSizeToFitWidth = YES;
            moneyField;
        });

    }
    return _calcultionImageVeiwRateTextField;
}
/**
 *  计算图片的滑块
 */


- (UISlider *)calcultionImageViewSlider{
    if (!_calcultionImageViewSlider) {
        _calcultionImageViewSlider = [[UISlider alloc]initWithFrame:CGRectMake(KWIDTH * 0.186, KHEIGHT * 0.18, KWIDTH * 0.22, KWIDTH * 0.05)];
        [_calcultionImageViewSlider setThumbImage:[LocalDataManager getImageWithSourceOfPath:@"试算_07"] forState:UIControlStateNormal];
        _calcultionImageViewSlider.maximumValue = 500;
        _calcultionImageViewSlider.minimumValue = 5;

        [_calcultionImageViewSlider addTarget:self action:@selector(action_slider:) forControlEvents:UIControlEventValueChanged];
    }
    return _calcultionImageViewSlider;
}
#pragma getters -- 展示
- (UIImageView *)showImageView{
    if (!_showImageView) {
        _showImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT )];
        _showImageView.image = [LocalDataManager getImageWithSourceOfPath:@"金融资讯详情"];
        _showImageView.userInteractionEnabled = YES;
    }
    return _showImageView;
}



/**
 *  数据源数组
 */
- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = [NSDictionary dictionary];
    }
    return _dataDictionary;
}

@end
